/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pokertimer;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

/**
 *
 * @author Bensch
 */
public class PokerStopwatch extends JFrame implements ActionListener, Runnable {
    private long startTime = 0;
    private final static java.text.SimpleDateFormat timerFormat = new java.text.SimpleDateFormat("mm : ss : SSS");
    private final JButton startStopButton= new JButton("Start/stop");
    private Thread updater;
    private boolean isRunning= false;
    private JTextField tsblind = new JTextField(10);
    private JTextField tbblind = new JTextField(10);
    
    
   
    private final Runnable displayUpdater= new Runnable()
    {
        @Override
        public void run()
        {
            displayElapsedTime(PokerStopwatch.this.startTime - System.currentTimeMillis());
        }
    };
       
    @Override
    public void actionPerformed(ActionEvent ae)
    {
        if(isRunning)
        {
            long elapsed= startTime - System.currentTimeMillis() ;
 
            isRunning= false;
            try
            {
                updater.join();
                // Wait for updater to finish
            }
            catch(InterruptedException ie) {}
            displayElapsedTime(elapsed);
            // Display the end-result
        }   
        else
        {
            startTime = 1*60*1000 + System.currentTimeMillis();
            
            isRunning= true;
            updater= new Thread(this);
            updater.start();
        }
    }
    private void displayElapsedTime(long elapsedTime)
    {
        startStopButton.setText(timerFormat.format(new java.util.Date(elapsedTime)));
    }
    
    @Override
    @SuppressWarnings("SleepWhileInLoop")
    public void run()
    {
        try
        {
            while(isRunning)
            {
                SwingUtilities.invokeAndWait(displayUpdater);
                Thread.sleep(50);
            }
        }
        catch(java.lang.reflect.InvocationTargetException ite)
        {
            ite.printStackTrace(System.err);
            // Should never happen!
        }
        catch(InterruptedException ie) {}
        // Ignore and return!
        }
    @SuppressWarnings("LeakingThisInConstructor")
        public PokerStopwatch()
        {   
            startStopButton.addActionListener(this);
            setLayout(new FlowLayout());
            setTitle("Pokerwatch");
            add(new JLabel("Small Blind:"));
            add(tsblind);
            add(new JLabel("Big Blind"));
            add(tbblind);
            add(startStopButton);
            setSize(800,100);
            setVisible(true);
        }
        public static void main(String[] arg)
        {
            new PokerStopwatch().addWindowListener(new WindowAdapter()
            {
                @Override
                public void windowClosing(WindowEvent e)
                {
                    System.exit(0);
                }
            });
        }  
}
